package com.scujcc.ssm.Controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.scujcc.ssm.Config.Util.Result;
import com.scujcc.ssm.Entity.Book;
import com.scujcc.ssm.Service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/books")

public class BookController{

    @Autowired
    private IBookService bookService;

    @PostMapping
    Result insertBook(@RequestBody Book book){
        boolean isSave = bookService.save(book);

        return new Result(isSave,isSave ? "添加成功!^_^" : "添加失败！-_-!");
    }
    @DeleteMapping("{id}")
    Result deleteBook(@PathVariable Integer id){
        boolean isDelete = bookService.removeById(id);
        return new Result(isDelete);
    }

    @GetMapping("{id}")
    Result getBook(@PathVariable Integer id){
        Book book = bookService.getById(id);
        return new Result(true,book);
    }

    @GetMapping
    Result getAll(){
        return new Result(true,bookService.list());
    }
    @GetMapping("{currentPage}/{pageSize}")
    Result getPage(@PathVariable Integer currentPage,@PathVariable Integer pageSize){
        Page<Book> page = bookService.getPage(currentPage, pageSize);
        return new Result(true,page);
    }
    @PutMapping
    Result upDate(@RequestBody Book book){
        boolean update = bookService.updateById(book);
        return new Result(update);
    }

}
